(require 'dbus)

(defun dbus-devices-added-handler (device)
  (message "Device added: %s" device))

(defun dbus-devices-removed-handler (device)
  (message "Device removed: %s" device))

(defun dbus-devices ()
  (interactive)
  (dbus-register-signal :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
                        "org.freedesktop.Hal.Manager" "DeviceAdded"
                        'dbus-devices-added-handler)
  (dbus-register-signal :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
                        "org.freedesktop.Hal.Manager" "DeviceRemoved"
                        'dbus-devices-removed-handler))

(defun dbus-devices-properties (device)
  (interactive "sDevice: ")
  (save-excursion
    (switch-to-buffer (get-buffer-create device))
    (erase-buffer)
    (dolist (prop (dbus-call-method
                   :system "org.freedesktop.Hal"
                   (concat "/org/freedesktop/Hal/devices/" device)
                   "org.freedesktop.Hal.Device" "GetAllProperties"))
      (insert (format "%s = %s\n" (car prop) (prin1-to-string (cdr prop)))))))

(defun dbus-devices-list ()
  (interactive)
  (save-excursion
    (switch-to-buffer (get-buffer-create "*devices*"))
    (erase-buffer)
    (dolist (dev (dbus-call-method
                  :system "org.freedesktop.Hal"
                  "/org/freedesktop/Hal/Manager"
                  "org.freedesktop.Hal.Manager" "GetAllDevices"))
      (insert dev "\n"))))

(when window-system
  (dbus-devices))
